<?php
if($access_control != "924y39c9vns8r73"){ header("location:../"); }

$servico = new servico();
$readonly = '';
if(valida::numero($_GET["id"]) && $_GET["id"] != ''){
    $id = $_GET["id"];
    $servico->carregar($id);
    $readonly = "readonly class='readonly'";
}

if ($_POST["btn_salvar"] == "Salvar"){
	$msg_erro = '';
	$erro = 0;
        
	$servico->set_id_empresa($_SESSION["sgm_id_empresa"]);
	if (!$servico->set_nome( $_POST["nome"] )){ $erro = 1; $msg_erro["nome"]  = "Nome inválido"; }
	if (!$servico->set_tempo($_POST["tempo"])){ $erro = 1; $msg_erro["tempo"] = "Tempo inválido"; }
	if (!$servico->set_valor($_POST["valor"])){ $erro = 1; $msg_erro["valor"] = "Valor inválido"; }
	
	if($erro == 0){
            if($servico->salvar()){
                echo "
                <script>
                    alert('Servico cadastrado com Sucesso');
                    location.href = '?pag=servico';
                </script>";
                unset($servico);
            }else{
                echo "
                <script>
                    alert('Falha ao gravar os dados');
                </script>";
            }
        }else{
            echo "
            <script>
                alert('Preencha so campos corretamente');
            </script>";
        }
}

function mostraServicos(){
    $sql = "select * from sgm_servico where id_empresa = '".$_SESSION["sgm_id_empresa"]."' order by nome";
    $db = new db(config::$driver);
    $con = $db->conecta();
    $res = $db->query($sql, $con);
    $db->close($con);
    $cont = 0;
    $i = 0;
    while ($servico = $db->fetch_array($res)){
        
        if($cont == 0){
            $cont = 1;
            $class = "zb1";
        }else{
            $cont = 0;
            $class = "zb2";
        }
        
        echo "
        <tr class='$class'>";
        if($_SESSION["sgm_master"] == 1){
            echo "
                <td><a href='?pag=servicoSet&id=".$servico["id"]."'>".$servico["id"]."</a></td>
                <td><a href='?pag=servicoSet&id=".$servico["id"]."'>".$servico["nome"]."</a></td>
                <td><a href='?pag=servicoSet&id=".$servico["id"]."'>".$servico["tempo"]."</a></td>
                <td><a href='?pag=servicoSet&id=".$servico["id"]."'>".$servico["valor"]."</a></td>
            ";
        }else{
            echo "
                <td>".$servico["id"]."</td>
                <td>".$servico["nome"]."</td>
                <td>".$servico["tempo"]."</td>
                <td>".$servico["valor"]."</td>
            ";
        }
        echo "</tr>";
        $i++;
    }
    if($i == 0){
        echo "<tr><td colspan='4'>Não há serviços</td></tr>";
    }
}
?>
